package com.pshcp.doctor.api;

import com.framework.base.PageList;
import com.framework.base.ResultBuilder;
import com.framework.base.ResultDTO;
import com.framework.exception.BizException;
import com.framework.logaop.Log;
import com.framework.logaop.LogSourceEnum;
import com.framework.logaop.LogTypeEnum;
import com.framework.thread.ReqComTool;
import com.framework.tool.CommonTool;
import com.pshcp.doctor.ao.patientgroup.SaveDoctorPatientGroupAO;
import com.pshcp.doctor.tool.DoctorPatientGroupTool;
import com.pshcp.doctor.tool.DoctorSendMessageTool;
import com.pshcp.doctor.vo.patientgroup.DoctorPatientGroupVO;
import com.pshcp.domain.dto.DoctorPatientGroupDTO;
import com.pshcp.domain.model.DoctorPatient;
import com.pshcp.domain.model.DoctorPatientGroup;
import com.pshcp.domain.model.DoctorPatientGroupUser;
import com.pshcp.domain.model.DoctorSendMessageContent;
import com.pshcp.domain.model.DoctorSendMessagePatient;
import com.pshcp.service.DoctorPatientGroupService;
import com.pshcp.service.DoctorPatientGroupUserService;
import com.pshcp.service.DoctorPatientService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description: TODO
 * @Author: xiaoche
 * @Date: 2022/11/3 23:48
 */
@Api(tags = {"医生端-患者分组接口"})
@Slf4j
@RestController
@RequestMapping("/api/doctor/patientGroup")
public class DoctorPatientGroupAPI {

    @Autowired
    private DoctorPatientGroupService doctorPatientGroupService;

    @Autowired
    private DoctorPatientGroupUserService doctorPatientGroupUserService;

    @Autowired
    private DoctorPatientService doctorPatientService;

    @ApiOperation("获取患者分组列表")
    @Log(source = LogSourceEnum.APP, type = LogTypeEnum.SELECT, desc = "获取患者分组列表")
    @PostMapping("getDoctorPatientGroupList")
    public ResultDTO<List<DoctorPatientGroupVO>> getDoctorPatientGroupList() throws BizException {
        DoctorPatientGroupDTO doctorPatientGroupDTO = DoctorPatientGroupTool.buildGetDoctorPatientGroupList();
        PageList<DoctorPatientGroup> pageList = doctorPatientGroupService.list(doctorPatientGroupDTO);
        List<DoctorPatientGroup> items = pageList.getItems();
        List<DoctorPatientGroupVO> list = CommonTool.copyPropertiesList(items, DoctorPatientGroupVO.class);
        return ResultBuilder.success(list);
    }

    @ApiOperation("保存患者分组")
    @Log(source = LogSourceEnum.APP, type = LogTypeEnum.SELECT, desc = "保存患者分组")
    @PostMapping("saveDoctorPatientGroup")
    public ResultDTO<Long> saveDoctorPatientGroup(@RequestBody SaveDoctorPatientGroupAO saveDoctorPatientGroupAO) throws BizException {
        DoctorPatientGroupTool.checkSaveDoctorPatientGroup(saveDoctorPatientGroupAO);
        List<DoctorPatient> doctorPatientList = doctorPatientService.getDoctorPatientList(ReqComTool.getUserId(), Arrays.asList(saveDoctorPatientGroupAO.getPatientIds()));
        List<DoctorPatient> doctorPatient2RelationList = doctorPatientList.stream().filter(doctorPatient -> doctorPatient.getRelationFlag() == 1).collect(Collectors.toList());
        if (saveDoctorPatientGroupAO.getPatientIds() != null && saveDoctorPatientGroupAO.getPatientIds().length != doctorPatient2RelationList.size()) {
            return ResultBuilder.failure("10140002");
        }
        Long doctorPatientGroupId = null;
        if (saveDoctorPatientGroupAO.getId() == null) {
            DoctorPatientGroup saveDoctorPatientGroup = DoctorPatientGroupTool.buildSaveDoctorPatientGroup(saveDoctorPatientGroupAO);
            doctorPatientGroupId = doctorPatientGroupService.insert(saveDoctorPatientGroup);
        } else {
            DoctorPatientGroup doctorPatientGroup = doctorPatientGroupService.findById(saveDoctorPatientGroupAO.getId());
            DoctorPatientGroup updateDoctorPatientGroup = DoctorPatientGroupTool.buildSaveDoctorPatientGroup(doctorPatientGroup, saveDoctorPatientGroupAO);
            doctorPatientGroupService.update(updateDoctorPatientGroup);
        }
        List<DoctorPatientGroupUser> doctorPatientGroupUserList = doctorPatientGroupUserService.getDoctorPatientGroupUserList(ReqComTool.getUserId(), null, doctorPatientGroupId);
        List<DoctorPatientGroupUser> list = DoctorPatientGroupTool.buildSaveDoctorPatientGroupUserList(doctorPatientGroupId, doctorPatientGroupUserList, Arrays.asList(saveDoctorPatientGroupAO.getPatientIds()));
        doctorPatientGroupUserService.batchSaveDoctorPatientGroupUser(list);
        return ResultBuilder.success(null);
    }
}
